package com.sanjin.utils;

import com.sanjin.entity.Admin;
import com.sanjin.entity.WxMiniUser;
import com.sanjin.service.AdminService;
import com.sanjin.service.WxMiniUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: sanjin
 * @date: 2019/5/15 21:00
 */
@Component
public class SecurityUtils {
    @Autowired
    private WxMiniUserService wxMiniUserService;

    @Autowired
    private AdminService adminService;

    public WxMiniUser getWxMiniUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String openid = (String) authentication.getPrincipal();
        return wxMiniUserService.queryWxMiniUserByOpenid(openid);
    }

    public Admin getAdmin() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = (String) authentication.getPrincipal();
        return adminService.queryAdminByUsername(username);
    }

}
